//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import com.dianxinos.acceleratecore.xlib.xlib.intf.IXObject;
import com.tools.acceleratecore.xlib.xlib.intf.IXFactory;
import com.tools.acceleratecore.xlib.xlib.intf.IXManager;

import java.util.HashMap;
import java.util.Map;

public abstract class XFactory implements IXFactory {
    protected Map<Class<?>, XFactory.XFactoryImplementMap> xFactoryInterfaceMap = new HashMap<>();


    @Override
    public IXObject createInstance(Class<?> classInterface) {
        return this.createInstance(classInterface, (Class<?>) null);
    }


    @Override
    public IXObject createInstance(Class<?> classInterface, Class<?> classImplement) {
        if (classInterface == null || !IXObject.class.isAssignableFrom(classInterface)) {
            return null;
        }

        XFactory.XFactoryImplementMap xFactoryImplementMap = this.findImplementMap(classInterface);
        if (xFactoryImplementMap == null) {
            return null;
        }

        int nImplementPos = this.findImplementPos(xFactoryImplementMap, classImplement);
        if (-1 == nImplementPos) {
            return null;
        }

        IXObject iXObject = null;
        if (IXManager.class.isAssignableFrom(classInterface)) {
            synchronized (XFactory.class) {
                if (xFactoryImplementMap.mArrayIXObject[nImplementPos] != null) {
                    iXObject = xFactoryImplementMap.mArrayIXObject[nImplementPos];
                } else {
                    try {
                        iXObject = (IXObject) xFactoryImplementMap.mArrayClassImplement[nImplementPos].newInstance();
                        xFactoryImplementMap.mArrayIXObject[nImplementPos] = iXObject;
                    } catch (Exception var10) {
                        var10.printStackTrace();
                    }
                }
            }
        } else {
            synchronized (XFactory.class) {
                try {
                    iXObject = (IXObject) xFactoryImplementMap.mArrayClassImplement[nImplementPos].newInstance();
                } catch (Exception var8) {
                    var8.printStackTrace();
                }
            }
        }

        return iXObject;
    }

    private XFactory.XFactoryImplementMap findImplementMap(Class<?> classInterface) {
        return classInterface == null ? null : xFactoryInterfaceMap.get(classInterface);
    }


    private int findImplementPos(XFactory.XFactoryImplementMap xFactoryImplementMap, Class<?> classImplement) {
        if (xFactoryImplementMap == null || xFactoryImplementMap.mArrayClassImplement == null || xFactoryImplementMap.mArrayClassImplement.length == 0) {
            return -1;
        }

        if (classImplement == null) {
            return 0;
        }

        for (int nIndex = 0; nIndex < xFactoryImplementMap.mArrayClassImplement.length; ++nIndex) {
            if (classImplement == xFactoryImplementMap.mArrayClassImplement[nIndex]) {
                return nIndex;
            }
        }

        return -1;
    }

    protected class XFactoryImplementMap {
        public Class<?>[] mArrayClassImplement = null;
        public IXObject[] mArrayIXObject = null;

        public XFactoryImplementMap(Class<?>[] arrayClassImplement, IXObject[] arrayIXObject) {
            this.mArrayClassImplement = arrayClassImplement;
            this.mArrayIXObject = arrayIXObject;
        }
    }
}
